package com.ddone.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ddone.finance.pojo.entity.UserInfo;
import com.ddone.finance.pojo.entity.UserLoginRecord;
import com.ddone.finance.mapper.UserLoginRecordMapper;
import com.ddone.finance.service.UserLoginRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 用户登录记录表 服务实现类
 * </p>
 *
 * @author ddone
 * @since 2024-04-10
 */
@Service
public class UserLoginRecordServiceImpl extends ServiceImpl<UserLoginRecordMapper, UserLoginRecord> implements UserLoginRecordService {
    @Override
    public void saveLoginLog(UserInfo userInfo, String ip) {
        UserLoginRecord userLoginRecord = new UserLoginRecord();
        userLoginRecord.setUserId(userInfo.getId());
        userLoginRecord.setIp(ip);
        userLoginRecord.setLoginType(StringUtils.isEmpty(userInfo.getOpenid())? 1:2);
        baseMapper.insert(userLoginRecord);
    }

    @Override
    public List<UserLoginRecord> listLatest50(Long userId) {
        LambdaQueryWrapper<UserLoginRecord> wrapper = Wrappers.lambdaQuery(UserLoginRecord.class);
        wrapper.eq(UserLoginRecord::getUserId, userId)
                .orderByDesc(UserLoginRecord::getId)
                .last("limit 50");
        return baseMapper.selectList(wrapper);
    }
}
